package edu.dlnu.gifttalk;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import edu.dlnu.gifttalk.category.CategoryFragment;
import edu.dlnu.gifttalk.home.HomeFragment;
import edu.dlnu.gifttalk.profile.ProfileFragment;
import edu.dlnu.gifttalk.rank.RankFragment;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//首页下面的四个按钮
    private View mTabHome;
    private View mTabRank;
    private View mTabCategory;
    private View mTabProfile;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView () {
        //初始化按钮控件
        mTabHome = findViewById(R.id.tab_home);
        mTabRank = findViewById(R.id.tab_rank);
        mTabCategory = findViewById(R.id.tab_category);
        mTabProfile = findViewById(R.id.tab_profile);
 //设计点击事件
        mTabHome.setOnClickListener(this);
        mTabRank.setOnClickListener(this);
        mTabCategory.setOnClickListener(this);
        mTabProfile.setOnClickListener(this);
        //通过代码点击第一个按钮（首页按钮）
        mTabHome.performClick();
    }

    @Override
    public void onClick (View v) {

        resetTabStatus();
        //点击的按钮设置为true，红色
        v.setSelected(true);
        //定义碎片管理器，用来管理某个fragment
        FragmentManager fm = getSupportFragmentManager();
        //定义fragment事物，显示或隐藏
        FragmentTransaction ft = fm.beginTransaction();
        //switch判断底部
        switch (v.getId()) {
            case R.id.tab_home:
                ft.replace(R.id.fragment_content, new HomeFragment());
                break;
            case R.id.tab_rank:
                ft.replace(R.id.fragment_content, new RankFragment());
                break;
            case R.id.tab_category:
                ft.replace(R.id.fragment_content, new CategoryFragment());
                break;
            case R.id.tab_profile:
                ft.replace(R.id.fragment_content, new ProfileFragment());
                break;
        }
        //提交事务上面的显示功能才会生效 显示到R.id.fragment_content的控件上
        ft.commit();
    }
//用来冲之所有按钮的状态，把控件的选择状态设置为both
    private void resetTabStatus(){
        //用来重置所有按钮的状态
        mTabHome.setSelected(false);
        mTabRank.setSelected(false);
        mTabCategory.setSelected(false);
        mTabProfile.setSelected(false);
    }
}
